/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Connect4Frame.java
 * La classe si occupa di creare la finestra principale del gioco,
 * contiene il pannello del menu, il tavolo da gioco e il pannello
 * delle statistiche
 *
 * Created on 27-apr-2010, 12.17.10
 */

package connectFour.GUI;


import connectFour.Controller;
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import connectFour.ControllerInterface;
import javax.swing.JCheckBoxMenuItem;
import javax.swing.JDialog;
import javax.swing.JMenuItem;


/**
 *
 * @author Ross
 */
public class Connect4Frame extends javax.swing.JFrame implements ActionListener {



    private Menu menu;
    private GameBoard board;
    private Statistic statistic;
    private ControllerInterface controller;
    private JDialog aboutDialog;


    /** Creates new form Connect4Frame */

    
    //DEVE OSSERVARE STATISTIC PER OTTENERE LE NUOVE STATISTICHE
    public Connect4Frame(String title) {
        super(title);

        myInitComponent();

        initComponents();//metodo del costruttore grafico di netbeans
        
        setComponentProperty();

    }

    /**
     * Inizializzazione manuale dei componenti
     */

    private void myInitComponent() {
        controller=new Controller();
        board = new GameBoard(controller);
        menu=new Menu();
        statistic=new Statistic();
        aboutDialog=new AboutDialog(this, true);

        getContentPane().add(menu, BorderLayout.LINE_START);
        getContentPane().add(board, BorderLayout.CENTER);
        getContentPane().add(statistic, BorderLayout.LINE_END);

    }

    /**
     * settaggio proprietà e listener
     */
    private void setComponentProperty() {
        this.setLocationRelativeTo(null);
        aboutDialog.setLocationRelativeTo(menu);
        aboutMenuItem.addActionListener(this);
        //registrazione all'evento del bottone start game
        menu.registerToStartButton(this);
        restartMenuItem.addActionListener(this);
        enableNextButton.addActionListener(this);
        
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        Title = new javax.swing.JLabel();
        jMenuBar1 = new javax.swing.JMenuBar();
        fileMenu = new javax.swing.JMenu();
        restartMenuItem = new javax.swing.JMenuItem();
        nextButtonMenu = new javax.swing.JMenu();
        enableNextButton = new javax.swing.JCheckBoxMenuItem();
        helpMenu = new javax.swing.JMenu();
        aboutMenuItem = new javax.swing.JMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setResizable(false);

        Title.setFont(new java.awt.Font("Tahoma", 1, 24));
        Title.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        Title.setText("CONNECT FOUR");
        getContentPane().add(Title, java.awt.BorderLayout.PAGE_START);

        fileMenu.setText("File");

        restartMenuItem.setText("Rigioca");
        restartMenuItem.setName("restart"); // NOI18N
        fileMenu.add(restartMenuItem);

        jMenuBar1.add(fileMenu);

        nextButtonMenu.setText("Pulsante Next");

        enableNextButton.setText("Abilita");
        enableNextButton.setToolTipText("Spunta per abilitare il pulsante Next");
        enableNextButton.setName("enableNextButton"); // NOI18N
        nextButtonMenu.add(enableNextButton);

        jMenuBar1.add(nextButtonMenu);

        helpMenu.setText("?");

        aboutMenuItem.setText("about");
        aboutMenuItem.setName("about"); // NOI18N
        helpMenu.add(aboutMenuItem);

        jMenuBar1.add(helpMenu);

        setJMenuBar(jMenuBar1);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
       
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Connect4Frame("Connect Four").setVisible(true);
            }
        });

    }


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel Title;
    private javax.swing.JMenuItem aboutMenuItem;
    private javax.swing.JCheckBoxMenuItem enableNextButton;
    private javax.swing.JMenu fileMenu;
    private javax.swing.JMenu helpMenu;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JMenu nextButtonMenu;
    private javax.swing.JMenuItem restartMenuItem;
    // End of variables declaration//GEN-END:variables


    /**
     * metodo invocato quando viene cliccato il pulsante startGame
     * oppure se viene cliccato il menuItem "about" o se viene cliccato restart
     * @param e
     */
    public void actionPerformed(ActionEvent e) {
            Component source=(Component) e.getSource();
            if(source.getName().equals("restart")){
                this.reset();
            }else if(source.getName().equals("about")){

                aboutDialog.setVisible(true);

            }
            else if(source.getName().equals("enableNextButton")){

                JCheckBoxMenuItem item=(JCheckBoxMenuItem) source;
                board.setUsesNextButton(item.isSelected());
             

            }else{
                startGame();
            }
    }

    /**
     * invocato quando viene cliccato il pulsante inizia!
     * Inizializza la partita
     */
    private void startGame() {
        String yellowPlayer = menu.getYellowPlayer();
        String redPlayer = menu.getRedPlayer();
        menu.setEnableInitGame(false);
        board.setYellowType(yellowPlayer);
        board.setRedType(redPlayer);
        board.setUsesNextButton(enableNextButton.isSelected());
        controller.initGame(yellowPlayer, menu.getYellowPlayerHorizont(), redPlayer, menu.getRedPlayerHorizont(), statistic);
        if (yellowPlayer.equals(ControllerInterface.human) && !redPlayer.equals(ControllerInterface.human)) {
            board.enableHumanVsCpuButton();
            statistic.setEnableRed(true);
        }
        if (!yellowPlayer.equals(ControllerInterface.human) && redPlayer.equals(ControllerInterface.human)) {
            board.enableCpuVsHumanButton();
            statistic.setEnableYellow(true);
            if (!enableNextButton.isSelected())
                board.actionPerformed(null);
        }
        if (!yellowPlayer.equals(ControllerInterface.human) && !redPlayer.equals(ControllerInterface.human)) {
            board.enableCpuVsCpuButton();
            statistic.setEnableYellow(true);
            statistic.setEnableRed(true);
        }
        if(yellowPlayer.equals(ControllerInterface.human) && redPlayer.equals(ControllerInterface.human))
            board.enalbeHumanVsHumanButton();
        
        
       
    }



    /**
     * Resetta la partita corrente riabilitando il menu di scelta
     */
    private void reset(){
       
        menu.setEnable(true);
        this.remove(board);
        board=new GameBoard(controller);
        this.add(board);
        statistic.cleanStatistics();
        statistic.setEnable(false);
        this.validate();
    }

}
